<?php

/**
 * 新闻
 */

namespace app\api\controller\course;

use app\api\controller\Common;

class Course extends Common
{
    protected $data;
    public function initialize()
    {
        parent::initialize();
        $this->data = $this->request->except(['sign', 'accessKey', 'token']);
    }

    public function list()
    {
        $this->checkUserLogin(); // 验证登录
        $user_id = $this->user['id'];
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 10;
        $paginate = ['list_rows' => $pageSize, 'page' => $page];

        $category = input('category', 1);
        $title = $this->data['title'];
        $sort_data = $this->data['sort'];
        $where = [];
        $where[] = ['a.status', '=', 1];
        $where[] = ['a.del', '=', 0];
        if ($category) {
            $where[] = ['a.category', '=', $category];
        }
        if ($title) {
            $where[] = ['a.title', 'like', '%' . $title . '%'];
        }
        // 排序方式
        if ($sort_data) {
            $sort_arr = [1 => 'a.hit', 2 => 'a.id'];
            $sort = $sort_arr[$sort_data];
        } else {
            $sort = 'a.hit';
        }
        $list = db('course')->alias('a')
            ->field('a.id,a.title,a.pic,a.type,a.price,a.item_num,a.create_time')
            ->where($where)->order($sort, 'desc')->paginate($paginate)->toArray();
        if (isset($list['data'])) {
            foreach ($list['data'] as $key => $val) {
                if($val['type'] == 2){
                    $order = db('order_info')->alias('a')->leftJoin('order_course b', 'a.id=b.order_id')->where(['b.user_id' => $user_id, 'b.course_id' => $val['id'], 'a.pay_status'=>1])->find();
                    if($order){
                        $list['data'][$key]['pay_status'] = 1;
                    }
                    else{
                        $list['data'][$key]['pay_status'] = 0;
                    }
                }
                else{
                    $list['data'][$key]['pay_status'] = 0;
                }
            }
        }
        return $this->showSuccess('课程列表', $list);
    }

    public function detail()
    {
        $id = input('id');
        $password = input('password', '');
        $detail = db('course')->field('id,title,type,pic,content,create_time,password,item_num')->where('id', $id)->find();
        
        if($detail['type'] == 2) {
            $this->checkUserLogin(); // 验证登录
            $user_id = $this->user['id'];
            $order = db('order_info')->alias('a')
                ->leftJoin('order_course b', 'a.id=b.order_id')->where(['b.user_id'=>$user_id, 'b.course_id'=>$id])->find();
            if(empty($order)){
                return $this->showError('请购买课程', '402');
            }
            elseif($order['pay_status'] != 1){
                return $this->showError('请支付课程订单', '403');
            }
        } 
        elseif ($detail['type'] == 3) {
            $this->checkUserLogin(); // 验证登录
            $user_type = $this->user['user_type'];
            if ($user_type == 1) {
                return $this->showError('请注册会员', '405');
            }
        }
        elseif ($detail['type'] == 4) {
            if (md5($password) != $detail['password']) {
                return $this->showError('密码错误', '401');
            }
        }
        if ($detail) {
            $uid = $this->user['id'];
            db('course')->where('id', $id)->setInc('hit'); // 浏览量+1
            $detail['format_data'] = $this->format_date($detail['create_time']);
            $detail['content'] = str_replace('src="', 'src="' . config('img_host'), $detail['content']);

            $item = db('course_item')
            ->field('id,title,play_url,hit,create_time')
            ->where('course_id', $detail['id'])->order('sort')->select();
            $detail['item'] = $item;

            $goods = db('course_goods')
            ->field('id,goods_id,name,pic,price')
            ->where('course_id', $detail['id'])->order('sort')->select();
            $detail['goods'] = $goods;

        } 

        return $this->showSuccess('课程详情', $detail);
    }




}
